package org.monkey.example.request.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;
import org.monkey.common.cbs.util.PageConvertUtil;
import org.monkey.common.constant.entity.ErrorResultEnum;
import org.monkey.common.constant.exception.BaseException;
import org.monkey.common.constant.util.DataUtil;
import org.monkey.example.request.dao.RequestInfoDao;
import org.monkey.example.request.entity.convert.RequestInfoConvert;
import org.monkey.example.request.entity.dto.RequestInfoDTO;
import org.monkey.example.request.entity.form.RequestInfoAddForm;
import org.monkey.example.request.entity.form.RequestInfoListForm;
import org.monkey.example.request.entity.form.RequestInfoPageForm;
import org.monkey.example.request.entity.form.RequestInfoSaveForm;
import org.monkey.example.request.entity.form.RequestInfoUpdateForm;
import org.monkey.example.request.entity.po.RequestInfoPO;
import org.monkey.example.request.service.RequestInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RequestInfoServiceImpl implements RequestInfoService {

  @Autowired
  RequestInfoDao requestInfoDao;
  @Autowired
  RequestInfoConvert requestInfoConvert;

  @Override
  public RequestInfoDTO getById(Long id, boolean nullThrow) {
    RequestInfoPO requestInfoPo = requestInfoDao.selectById(id);
    if (requestInfoPo == null && nullThrow) {
      throw new BaseException(ErrorResultEnum.DATA_NOT_FOUNT);
    }
    return requestInfoConvert.po2Dto(requestInfoPo);
  }

  @Override
  public RequestInfoDTO add(RequestInfoAddForm addForm) {
    RequestInfoDTO requestInfoDto = requestInfoConvert.addForm2Dto(addForm);
    RequestInfoPO requestInfoPo = requestInfoConvert.dto2Po(requestInfoDto);
    requestInfoDao.insert(requestInfoPo);
    return requestInfoConvert.po2Dto(requestInfoPo);
  }

  @Override
  public RequestInfoDTO update(RequestInfoUpdateForm updateForm) {
    RequestInfoDTO requestInfoDto = requestInfoConvert.updateForm2Dto(updateForm);
    RequestInfoPO requestInfoPo = requestInfoConvert.dto2Po(requestInfoDto);
    requestInfoDao.updateById(requestInfoPo);
    return requestInfoConvert.po2Dto(requestInfoPo);
  }

  @Override
  public RequestInfoDTO save(RequestInfoSaveForm saveForm) {
    RequestInfoDTO requestInfoDto = requestInfoConvert.saveForm2Dto(saveForm);
    RequestInfoPO requestInfoPo = requestInfoConvert.dto2Po(requestInfoDto);
    if (requestInfoDto.getId() == null) {
      requestInfoDao.insert(requestInfoPo);
    } else {
      requestInfoDao.updateById(requestInfoPo);
    }
    return requestInfoConvert.po2Dto(requestInfoPo);
  }

  @Override
  public List<RequestInfoDTO> listAll() {
    List<RequestInfoPO> pos = requestInfoDao.selectList(new QueryWrapper<>());
    return DataUtil.colTypeChange(pos, po -> requestInfoConvert.po2Dto(po));
  }

  @Override
  public IPage<RequestInfoDTO> page(RequestInfoPageForm pageForm) {
    Page<RequestInfoPO> page = new Page<>(pageForm.getPageNumber(),
        pageForm.getPageSize());
    IPage<RequestInfoPO> iPage = requestInfoDao
        .page(page, pageForm);
    return PageConvertUtil.convert(iPage, po -> requestInfoConvert.po2Dto(po));
  }

  @Override
  public List<RequestInfoDTO> list(RequestInfoListForm listForm) {
    List<RequestInfoPO> pos = requestInfoDao.list(listForm);
    return DataUtil.colTypeChange(pos, po -> requestInfoConvert.po2Dto(po));
  }

  @Override
  public Boolean delete(Long id) {
    requestInfoDao.deleteById(id);
    return true;
  }
}
